package com.example.fragment;

import android.content.res.Resources;
import android.graphics.BitmapFactory;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

public class RvAd extends RecyclerView.Adapter<RvAd.VH> {
    private final List<Info> list;
    private Resources resources;

    public RvAd(List<Info> list, Resources resources) {
        this.list = list;
        this.resources = resources;
    }

//    public RvAd(List<Info> list) {
//        this.list = list;
//    }

    @NonNull
    @Override
    public VH onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new VH(LayoutInflater.from(parent.getContext()).inflate(R.layout.rv_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull VH holder, int position) {
        Info info = list.get(position);
        holder.img1.setImageBitmap(BitmapFactory.decodeResource(resources, info.getId()));
        holder.tv.setText(info.getName());
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class VH extends RecyclerView.ViewHolder {
        private ImageView img1;
        private TextView tv;

        private void initView(View vh) {
            img1 = (ImageView) vh.findViewById(R.id.img1);
            tv = (TextView) vh.findViewById(R.id.tv);
        }

        public VH(@NonNull View itemView) {
            super(itemView);
            initView(itemView);
        }
    }
}
